//
//  LCTabBarViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/20.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTabBarViewController.h"
#import "LCNavigationViewController.h"

#import "LCCouponViewController.h"
#import "LCTaskBaseViewController.h"
#import "LCGlobalBaseViewController.h"
#import "LCMineViewController.h"

@interface LCTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation LCTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTabBarConfig];
}

- (void)setTabBarConfig {
    
    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#FFFFFF"]]];
    
    if (@available(iOS 15.0, *)) {
        UITabBarAppearance * apper = [[UITabBarAppearance alloc] init];
        [apper configureWithOpaqueBackground];
        apper.backgroundImage = [UIImage imageWithColor:[UIColor colorWithHexString:@"#FFFFFF"]];
        [UITabBar appearance].standardAppearance = apper;
        [UITabBar appearance].scrollEdgeAppearance = [UITabBar appearance].standardAppearance;
    }
    
    self.delegate = self;
    self.tabBar.tintColor = [UIColor colorWithHexString:@"#16A1E3"];
    [self addChildViewControllers];
    self.selectedIndex = 0;
}

//添加子控制器
- (void)addChildViewControllers {
    //
    [self addChildVC:[LCCouponViewController xx_instantiateFromStoryboardNamed:@"Coupon"] ImgName:@"lc_tabbar_home" SelImgName:@"lc_tabbar_home_sel" Title:@"首页"];
    [self addChildVC:[LCTaskBaseViewController xx_instantiateFromStoryboardNamed:@"Task"] ImgName:@"lc_tabbar_task" SelImgName:@"lc_tabbar_task_sel" Title:@"应用"];
    [self addChildVC:[LCGlobalBaseViewController xx_instantiateFromStoryboardNamed:@"Global"] ImgName:@"lc_tabbar_global" SelImgName:@"lc_tabbar_global_sel" Title:@"全域推"];
    [self addChildVC:[LCMineViewController xx_instantiateFromStoryboardNamed:@"Mine"] ImgName:@"lc_tabbar_mine" SelImgName:@"lc_tabbar_mine_sel" Title:@"我的"];
}

- (void)addChildVC:(UIViewController *)childVC ImgName:(NSString *)imgName SelImgName:(NSString *)selImgName Title:(NSString *)title {
    
    LCNavigationViewController * navVC = [[LCNavigationViewController alloc] initWithRootViewController:childVC];
    navVC.tabBarItem.title = title;
    if (imgName) {
        navVC.tabBarItem.image = [UIImage lc_originalImage:imgName];
        navVC.tabBarItem.selectedImage =  [UIImage lc_originalImage:imgName];
    }
    if (selImgName) {
        navVC.tabBarItem.selectedImage = [UIImage lc_originalImage:selImgName];
    }
    
    UIColor * normalColor = [UIColor colorWithHexString:@"#999999"];
    UIColor * selectColor = [UIColor colorWithHexString:@"#16A1E3"];
    
    if (@available(iOS 12.0, *)) {
        navVC.tabBarItem.image = [UIImage lc_originalImage:imgName];
        navVC.tabBarItem.selectedImage = [UIImage lc_originalImage:selImgName];
        [navVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:normalColor} forState:UIControlStateNormal];
        [navVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:selectColor} forState:UIControlStateSelected];
    }
    
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = UITabBarAppearance.new;
        NSMutableParagraphStyle *par = [[NSMutableParagraphStyle alloc]init];
        par.alignment = NSTextAlignmentCenter;
        UITabBarItemStateAppearance *normal = appearance.stackedLayoutAppearance.normal;
        UITabBarItemStateAppearance *selected = appearance.stackedLayoutAppearance.selected;
        if (normal) {
            normal.titleTextAttributes = @{NSForegroundColorAttributeName:normalColor};
        }
        if (selected) {
            selected.titleTextAttributes = @{NSForegroundColorAttributeName:selectColor};
        }
        self.tabBar.standardAppearance = appearance;
    }
    
    [self addChildViewController:navVC];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
